package com.iswust.visual.controller;


import com.iswust.visual.model.CommonResult;
import com.iswust.visual.model.vo.*;
import com.iswust.visual.service.BayonetDataService;
import com.iswust.visual.service.CityAqiService;
import com.iswust.visual.service.SiteMonitoringDataService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author swust
 * @since 2023-02-17
 */
@RestController
@RequestMapping("/vis/bayonetData")
public class BayonetDataController {

    @Autowired
    private BayonetDataService bayonetDataService;
    @Autowired
    private SiteMonitoringDataService siteMonitoringDataService;
    @Autowired
    private CityAqiService cityAqiService;
    /**
     * 获取多轴散点图数据
     *
     * @param dateTimeVo
     * @return
     */
    @GetMapping("/scatter")
    @SneakyThrows
    public CommonResult getScatterData(DateTimeVo dateTimeVo) {
        List<ScatterData> scatterData;
        scatterData = bayonetDataService.getByMonth(dateTimeVo);
        List<List<Integer>> scatters = new ArrayList<>();
        for (int j = 0; j < ScatterData.class.getDeclaredFields().length; j++) {
            for (int i = 0; i < scatterData.size(); i++) {
                List<Integer> scatter = new ArrayList<>();
                final Field[] declaredFields = scatterData.get(i)
                        .getClass()
                        .getDeclaredFields();
                declaredFields[j].setAccessible(true);
                scatter.add(j);
                scatter.add(i);
                scatter.add(Integer.valueOf(declaredFields[j].get(scatterData.get(i))
                        .toString()));
                scatters.add(scatter);
            }
        }
        final ScatterDataVo scatterDataVo = new ScatterDataVo();
        scatterDataVo.setData(scatters);
        List<String> days = new ArrayList<>();
        for (int i = 1; i <= scatterData.size(); i++) {
            days.add(String.valueOf(i));
        }
        scatterDataVo.setDays(days);
        return CommonResult.success(scatterDataVo);
    }

    /**
     * 桑基图
     * @param dateTimeVo
     * @param bayonetName
     * @return
     */
    @GetMapping("/sankey")
    public CommonResult getSankeyData(DateTimeVo dateTimeVo, @RequestParam(value = "name", required = false) String bayonetName) {
        if (bayonetName == null || "".equals(bayonetName)) {
            final SankeyVo bayonetByDate = bayonetDataService.getBayonetByDate(dateTimeVo);
            return CommonResult.success(bayonetByDate);
        } else {
            final SankeyVo bayonetByDate = bayonetDataService.getBayonetByDate(dateTimeVo, bayonetName);
            return CommonResult.success(bayonetByDate);
        }
    }

    /**
     * 日历图
     * @param dateTimeVo
     * @return
     */
    @GetMapping("/calender")
    public CommonResult getCalenderData(DateTimeVo dateTimeVo) {
        final List<CalenderVo> calenderData = cityAqiService.getCalenderData(dateTimeVo);
        return CommonResult.success(calenderData);
    }

    /**
     * 根据时间字符串cj返回当月数据返回卡口车流量数据
     *
     * @param dateTimeVo
     * @param bayonetName
     * @return
     */
    @GetMapping("/mainView")
    public CommonResult getMainViewData(DateTimeVo dateTimeVo) {
        final List<MainViewVo> mainViewDataByDate = bayonetDataService.getMainViewDataByDate(dateTimeVo);
        return CommonResult.success(mainViewDataByDate);
    }

    /**
     * 雷达图
     * @param dateTimeVo
     * @return
     */
    @GetMapping("/radar")
    public CommonResult getRadarData(DateTimeVo dateTimeVo) {
        List<RadarVo> radarVos = siteMonitoringDataService.getRadarData(dateTimeVo);
        return CommonResult.success(radarVos);
    }

    /**
     * 风向图
     * @param dateTimeVo
     * @return
     */
    @GetMapping("/wind")
    public CommonResult getWindGradeData(DateTimeVo dateTimeVo) {
        final List<WindVo> windData = cityAqiService.getWindData(dateTimeVo);
        return CommonResult.success(windData);
    }
}

